from PySide6.QtWidgets import QFileDialog,QMessageBox
import os,shutil,pandas as pd

class OperationModel:

    # 模板下载
    def downloadmodleFun(self,file_name):
        model_file_name =str(file_name)
        model_file_path = os.path.join('state','model',model_file_name)
        save_path,_ = QFileDialog.getSaveFileName(None,'下载模板',model_file_name,f"(*.{model_file_name.split('.')[-1]})")
        # 判断路径是否存在
        shutil.copy2(model_file_path, save_path)
        QMessageBox.information(None,'下载成功','恭喜你下载成功！')

    # 导入数据
    def importdataFun(self, names, dtypes, parse_dates=None):
        if parse_dates is None:
            parse_dates = parse_dates
        url,a = QFileDialog.getOpenFileUrl(None,'导入数据')
        try:
            data = pd.read_excel(url.toLocalFile(), sheet_name=0, header=1,
                                 names=names,
                                 dtype=dtypes,
                                 parse_dates=parse_dates
                                 ).drop('idx', axis=1)
            return data
        except:
            QMessageBox.critical(None,'导入失败','你没有选择文件！')
